import { defineStore } from "pinia"
import { CourseProgressReport, CourseProgressQuery } from "@/models/course"
import { courseApi } from "@/services"
import { toast } from "vue3-toastify"

const usePlayerStore  = defineStore('Player', {
    // state
    state:() =>({
        courseDetailObject: {} as any,
        courseProgress: '',
        playAuthObject: {} as any,
    }),
    // actions
    actions:{
        // 获取课程详情（JSONObject）
        async getCourseDetail(srcCourseId:string) {
            try{
                const res = await courseApi.getCourseDetail(srcCourseId)
                this.courseDetailObject = JSON.parse(res.detail)
                this.courseProgress = res.progress
            } catch(err) {
                toast.error('获取课程详情出现异常：'+ err);
            }
        },

        // 加入课程学习
        async joinCourse(srcCourseId: string) {
            try{
                const res = await courseApi.joinCourse(srcCourseId)
                return res ? res.result : false
            } catch(err) {
                toast.error('加入课程学习出现异常：'+ err);
                return false;
            }
        },
        // 上报课程进度
        async reportProgress(srcCourseId: string, seq: string, playSecond: number) {
            try{        
                const data : CourseProgressReport = {
                    srcCourseId: srcCourseId,
                    seq: seq,
                    playSecond: playSecond,
                }
                const res = await courseApi.reportProgress(data)
                return res ? res.result : false
            } catch(err) {
                toast.error('上报课程进度出现异常：'+ err);
                return false;
            }
        },

        // 查询课程进度
        async queryProgress(srcCourseId: string, seq: string) {
            try{        
                const data : CourseProgressQuery = {
                    srcCourseId: srcCourseId,
                    seq: seq,
                }
                const res = await courseApi.queryProgress(data)
                return (res && res.result && res.result[0]) ? (res.result[0].second || 0) : 0;
            } catch(err) {
                toast.error('查询课程进度出现异常：'+ err);
                return 0;
            }
        },

        // 授权播放
        async getPlayAuth(vid:string) {
            try{
                const res = await courseApi.getPlayAuth(vid)
                this.playAuthObject = JSON.parse(res.detail)
            } catch(err) {
                toast.error('授权播放出现异常：'+ err);
            }
        },

        // 获取课程最后学习章节
        getCourseLastSeq(srcCourseId:string) {
            return courseApi.getCourseLastSeq(srcCourseId);
        },

        // 设置课程最后学习章节
        setCourseLastSeq(srcCourseId:string, lastSeq: number) {
            courseApi.setCourseLastSeq(srcCourseId, lastSeq);
        },
    },
    getters:{
        courseDetail:(state) => state.courseDetailObject,
        playAuth:(state) => state.playAuthObject.playAuth,
    }
})

export default usePlayerStore